package com.example.shushiyunsst.fragment.main.mine.child

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.alibaba.fastjson.JSON
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.bean.UpDataVersionBean
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.BuildConfig
import com.example.shushiyunsst.R
import com.example.wallet.db.UserInfoManager
import com.lxj.xpopup.XPopup

class AboutUsFragment : BaseFragmentEx() {

    companion object {
        fun newInstance(): AboutUsFragment {
            val args = Bundle()
            val fragment = AboutUsFragment()
            fragment.arguments = args
            return fragment
        }
}

    override fun getLayoutId(): Any {
        return R.layout.about_us_fragment
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val upDataVersion: TextView = view.findViewById(R.id.upDataVersion)
        upDataVersion.setOnClickListener {
            requestData()
        }

        val app_loge: ImageView =view.findViewById(R.id.app_loge)
        app_loge.setImageResource(R.mipmap.app_icon_1)

        val versionName: TextView = view.findViewById(R.id.versionName)
        versionName.text = BuildConfig.VERSION_NAME

        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.title = "关于我们"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }


    }

    private fun requestData() {


        val maps = mapOf(
            "version" to BuildConfig.VERSION_NAME
        )
        RestClient.builder()
            .url(R.string.updateAppVersion)
            .raw(JSON.toJSONString(maps))
            .success {

                val obj = JSON.parseObject(it, UpDataVersionBean::class.java)

                if (obj.needToUpData()) {
                    val pop = UpDataVersionPop(_mActivity)
                    pop.setVersionBean(obj)
                    pop.setTarget(this)
                    XPopup.Builder(_mActivity)
//                        .enableShowWhenAppBackground(true)
                        .asCustom(pop)
                        .show()
                }else{
                    Toast.makeText(_mActivity,"已经是最新版本",Toast.LENGTH_SHORT).show()
                }

            }
            .build()
            .post()

    }
}